//
//  DDropIntoViewController.swift
//  Drop
//
//  Created by hushaohua on 2017/11/24.
//  Copyright © 2017年 init. All rights reserved.
//

import UIKit

class DDropIntoViewController: UIViewController {

    func configTapGesture() -> Void {
        self.enableTapToDismissKeyboard();
    }
    
    func didShow() -> Void {
        self.intoView.textField.text = "";
        self.intoViewPreseter.reloadTags();
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configTapGesture();
        self.edgesForExtendedLayout = .top;
        self.extendedLayoutIncludesOpaqueBars = true;
        self.automaticallyAdjustsScrollViewInsets = false;
        self.view.backgroundColor = UIColor.h_darkBlue();
        self.view.addSubview(self.intoView);
        self.intoView.backgroundColor = UIColor.clear;
        self.intoViewPreseter.view = self.intoView;
        if #available(iOS 11.0, *) {
            self.configForScroll()
        } else {
            // Fallback on earlier versions
        };
    }
    
    @available(iOS 11.0, *)
    func configForScroll() -> Void {
        self.intoView.collectionView.contentInsetAdjustmentBehavior = .never;
    }
    
    lazy var intoViewPreseter:DDropIntoViewPresenter = {
        var presenter = DDropIntoViewPresenter();
        presenter.customEndTimeClosure = { tag -> Void in
            self.showCustomEndTimeOf(tag);
        };
        return presenter;
    }();
    
    var backClosure:(() -> Void)?;
    func showCustomEndTimeOf(_ tag:Tag) -> Void{
        let drop = DDroppingManager.sharedManager().doingDrop;
        self.customEndTimeView.okClosure = { minutes in
            let date:Date = Date(timeInterval: TimeInterval(60 * minutes), since: (drop!.begin)! as Date)
            DDroppingManager.sharedManager().doingDrop?.tag = tag;
            DDroppingManager.sharedManager().finishAt(date);
            if (self.backClosure != nil){
                self.backClosure!();
            }
            self.customEndTimeView.dismiss();
        };
        self.customEndTimeView.show(drop: drop!, at: tag);
    }
    
    lazy var intoView:DDropIntoView = {
        var view:DDropIntoView = DDropIntoView(frame: CGRect(x:0,y:0,width:self.view.bounds.size.width,height:self.view.bounds.size.height));
        view.collectionView.contentInset = UIEdgeInsetsMake(84, 0, 44, 0);
        view.textField.backgroundColor = UIColor(displayP3Red: 30.0/255, green: 41.0/255, blue: 51.0/255, alpha: 1);
        return view;
    }();
    
    lazy var customEndTimeView:DDropsCustomEndTimeView = {
        var view:DDropsCustomEndTimeView = DDropsCustomEndTimeView(frame: CGRect(x:0,y:0,width:self.view.bounds.size.width,height:234));
        view.backgroundColor = UIColor.white;
        return view;
    }();

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent;
    }

}
